/*
 * scalar.h
 *
 *  Created on: Sep 17, 2008
 *      Author: lindenb
 */

#ifndef YNODE_H_
#define YNODE_H_
#include "lexyacc/scalar.h"
#ifdef  __cplusplus
extern "C" {
#endif

/**
 * classical comparaison operator
 * will be used in the yacc parser to define the
 * kind of comparaison
 */
enum YNode_Comparator
	{
	Y_NODE_EQ=1,//equal
	Y_NODE_NE=2,//not equal
	Y_NODE_LT=3,//lower than
	Y_NODE_GT=4,//greater than
	Y_NODE_LE=5,//lower or equal
	Y_NODE_GE=6//greator or equal
	};

/** the current context. Will be passed to the tree to get the parameters
 * needed to evaluate the tree
 */
typedef struct yContext_t
	{
	/** user data, may be whatever you want */
	void* userData;
	}YContext,*YContextPtr;


/**
 * A Node in the query-root
 */
typedef struct ynode_t
	{
	/** link to the first children (if any) */
	struct ynode_t* first;
	/** link to the second children (if any) */
	struct ynode_t* second;
	/** kind of comparator if this is a comparaison node */
	enum YNode_Comparator comparator;
	/** a constant , if this is a constant node */
	ScalarPtr constant;
	/** functor: how to eval this node ? */
	ScalarPtr (*eval)(const struct ynode_t* self,YContextPtr context);
	/** callback to free this node and its children */
	void (*free)(struct ynode_t* self);
	/** print this node */
	void (*print)(const struct ynode_t* node, FILE* out);
	}YNode,*YNodePtr;

/**
 * Delete a Node. will call node->free(node)
 * @param ptr the node to delete
 */
extern	void YNodeDelete(YNodePtr ptr);
/**
 * Initialize a node
 * useful function only for derived classes
 * @param ptr the node to initialize
 * @return the node initialized
 */
extern	YNodePtr YNodeInit(YNodePtr ptr);
extern YNodePtr yNodeMakeAND(YNodePtr a, YNodePtr b);
extern YNodePtr yNodeMakeOR(YNodePtr a, YNodePtr b);
extern YNodePtr yNodeMakeNOT(YNodePtr a);
extern YNodePtr yNodeMakeCompare(YNodePtr a, enum YNode_Comparator compare,YNodePtr b);
extern YNodePtr yNodeMakeConstant(ScalarPtr scalar);

#ifdef  __cplusplus
extern }
#endif

#endif /* YNODE_H_ */
